using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ST.Application.RoleServices;
using ST.Application.RoleServices.Dtos;

namespace Portal.Controllers
{
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;

        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }
        
        // GET
        public IActionResult Index()
        {
            return View();
        }
        
        public async Task<IActionResult> Edit(RoleDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = GetModelStateError()
                });
            }
            var result = false;
            if (await _roleService.GetAsync(dto.Id) == null)
            {
                result = await _roleService.InsertAsync(dto);
            }
            else
            {
                result = await _roleService.UpdateAsync(dto);
            }

            return Json(result ? new { Result = "Success" } : new { Result = "Faild" });
        }

        public  async Task<IActionResult> GetAllPageList(int startPage, int pageSize)
        {
            var result = await _roleService.GetAllPageListAsync(startPage, pageSize);
            return Json(result);
        }

        public async Task<IActionResult> DeleteMuti(string ids)
        {
            var idArray = ids.Split(',');
            try
            {
                var delIds = idArray.Select(Guid.Parse).ToList();
                await _roleService.DeleteBatchAsync(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public async Task<IActionResult> Delete(Guid id)
        {
            try
            {
                await _roleService.DeleteAsync(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public async Task<IActionResult> Get(Guid id)
        {
            var dto = await _roleService.GetAsync(id);
            return Json(dto);
        }

        /// <summary>
        /// 根据角色获取权限
        /// </summary>
        /// <returns></returns>
        public IActionResult GetMenusByRole(Guid roleId)
        {
            var dtls =_roleService.GetAllMenuListByRole(roleId);
            return Json(dtls);
        }

        public IActionResult SavePermission(Guid roleId, List<RoleMenuDto> roleMenus)
        {
            return Json(_roleService.UpdateRoleMenu(roleId, roleMenus) ? new { Result = "Success" } : new { Result = "Faild" });
        }
    }
}